package fonkin.com.wifisignapp.acticity.sign;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;


import org.feezu.liuli.timeselector.TimeSelector;

import java.text.SimpleDateFormat;
import java.util.Date;

import fonkin.com.wifisignapp.R;
import fonkin.com.wifisignapp.acticity.aboutMe.Procedure;
import fonkin.com.wifisignapp.retrofit.BaseRetrofit;
import fonkin.com.wifisignapp.utils.ReturnVo;
import fonkin.com.wifisignapp.utils.ShowToast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SignApply extends AppCompatActivity {


    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    EditText dayNum,applyScripts,bTime,eTime;
    String dateTimeStr;
    RadioGroup radioGroup;
    SharedPreferences preferences;
    String scheduleType="",bTimeStr,dayNumStr,eTimeStr,userId,userName,applyScriptsStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_apply);
        radioGroup = (RadioGroup)findViewById(R.id.modify_sex_check);
        dayNum = (EditText)findViewById(R.id.day_num);
        bTime = (EditText)findViewById(R.id.apply_begin_time);
        eTime = (EditText)findViewById(R.id.apply_end_time);
        applyScripts = (EditText)findViewById(R.id.apply_scripts);
        preferences =  PreferenceManager.getDefaultSharedPreferences(SignApply.this);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton viewById = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());
                scheduleType = viewById.getText().toString();
            }
        });
        long tenYears = 10L * 365 * 1000 * 60 * 60 * 24L;

        bTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                TimeSelector timeSelector = new TimeSelector(SignApply.this, new TimeSelector.ResultHandler() {
                    @Override
                    public void handle(String time) {
                        bTime.setText(time);
                    }
                }, "2018-04-28 00:00", "2030-01-01 23:59:59");
                timeSelector.show();

            }
        });



        eTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TimeSelector timeSelector = new TimeSelector(SignApply.this, new TimeSelector.ResultHandler() {
                    @Override
                    public void handle(String time) {
                        eTime.setText(time);
                    }
                }, "2015-01-01 00:00", "2018-12-31 23:59:59");
                timeSelector.show();

            }
        });
    }


    public void applyBtn(View view){
        bTimeStr = bTime.getText().toString();
        dayNumStr = dayNum.getText().toString();
        eTimeStr = eTime.getText().toString();
        userId = preferences.getString("user_id","");
        userName = preferences.getString("user_name","");
        applyScriptsStr = applyScripts.getText().toString();
        if ("".equals(dayNumStr)){
            ShowToast.ShowToastShort("请填写天数",SignApply.this);
        }else if ("".equals(bTimeStr)){
            ShowToast.ShowToastShort("请填写开始时间",SignApply.this);
        }else if("".equals(eTimeStr)){
            ShowToast.ShowToastShort("请填写结束时间",SignApply.this);
        }else if ("".equals(applyScriptsStr)){
            ShowToast.ShowToastShort("请填写说明",SignApply.this);
        }else if ("".equals(scheduleType)){

        }else{
            BaseRetrofit.GetBaseRetrofit().addApplicationList(userId,bTimeStr,dayNumStr,userName,eTimeStr,scheduleType,applyScriptsStr).enqueue(new Callback<ReturnVo<String>>() {
                @Override
                public void onResponse(Call<ReturnVo<String>> call, Response<ReturnVo<String>> response) {
                    if (response.isSuccessful()){
                        if (response.isSuccessful()&&response.body().getCode().equals(1)){
                            ShowToast.CenterPicToast("发起成功",SignApply.this,R.drawable.ic_check_circle_black_24dp);
                        }else {
                            ShowToast.CenterPicToast("发起失败", SignApply.this, R.drawable.ic_clear_black_24dp);
                        }
                    }
                }

                @Override
                public void onFailure(Call<ReturnVo<String>> call, Throwable t) {

                }
            });
        }

    }
}
